package com.fm.demo.joke

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fm.demo.R
import com.fm.demo.bean.JokeBean
import com.fm.demo.utils.DialogUtils

/**
 * @CreateData:     2019/11/29 14:43
 * @E-Mail:         709674174@qq.com
 * @Author:         lyy
 * @Description:
 */
class JokeItemAdapter : BaseQuickAdapter<JokeBean, BaseViewHolder>(R.layout.item_joke_layout), LoadMoreModule {
    private val mAdapters = HashMap<RecyclerView, JokeItemImageAdapter>()

    override fun convert(holder: BaseViewHolder, item: JokeBean) {
        item.let {
            val recyclerView = holder.getView<RecyclerView>(R.id.item_joke_rlv_image)
            var adapter = mAdapters[recyclerView]
            if (null == adapter) {
                adapter = JokeItemImageAdapter()
            }
            adapter.let {
                if (!item.images.isNullOrEmpty()) {
                    val images = item.images!!.split(",").dropLastWhile { it.isEmpty() }
                    adapter.setList(images)
                }
                it.setOnItemClickListener { adapter, _, position ->
                    DialogUtils.showImageDialog(context, adapter.data[position] as String)
                }
            }
            val layoutManager = GridLayoutManager(context, 3)
            recyclerView.layoutManager = layoutManager
            recyclerView.adapter = adapter
            Glide.with(context).load(item.header).apply(RequestOptions().centerCrop()).into(holder.getView(R.id.item_joke_iv_avatar))
            holder.setText(R.id.item_joke_tv_nickname, item.name)
                .setText(R.id.item_joke_tv_time, item.passtime)
                .setText(R.id.item_joke_tv_content, item.text)
        }
    }

}